package com.xkcyy.one.ai.trainable.tools;

import cn.hutool.extra.spring.SpringUtil;
import com.xkcyy.one.ai.common.annotation.Tool;
import com.xkcyy.one.ai.common.annotation.ToolParam;
import com.xkcyy.one.ai.common.tool.AbstractAutoDescTool;
import com.xkcyy.one.ai.llm.model.ToolDefinition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * 天气工具实现
 * 
 * @author yuand
 */
@Slf4j
@Tool(name = "weather", description = "获取指定城市的天气信息")
public class WeatherTool extends AbstractAutoDescTool {

    public WeatherTool(ToolDefinition definition) {
        super(definition);
    }

    /**
     * 获取天气信息
     * 
     * @param city 城市名称
     * @return 天气信息
     */
    public String getWeather(@ToolParam(description = "城市名称") String city) {
        try {
            RestTemplate restTemplate =  SpringUtil.getBean(RestTemplate.class);
            String url = String.format("https://api.weather.com/weather?city=%s", city);
            return restTemplate.getForObject(url, String.class);
        } catch (Exception e) {
            log.error("Error executing weather tool", e);
            throw new RuntimeException("Failed to execute weather tool", e);
        }
    }
}